<?php
/**
 * @version			$Id$
 * @author			Richard Asp
 * @package			Joomla
 * @subpackage		gnudoor
 * @copyright		(C) 2010 gnudoor LLC
 * @license      GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');

class GnudoorControllerPay extends GnudoorController
{

	
    function display() {
        parent::display();
    }
	function save() {
		$cid = JRequest::getVar('id');
		$invoice = JRequest::getVar('code');
		$userid = JRequest::getVar('iam');
		$model = $this->getModel('Pay');
		$value = $model->getValueFromProvider($cid);
		$this->_sendToPaypal($cid, $invoice, $userid, $this->_formatValue($value->value));
	}
	function notify() {
	
	}
	function cancel() {
		
	}
	function activate() {
		$cid = JRequest::getVar('cid');
		$txn_date = JRequest::getVar('payment_date');
		$txn_id = JRequest::getVar('txn_id');
		$txn_amount = JRequest::getVar('mc_gross');
		$custom = JRequest::getVar('custom');
		
        JRequest::setVar( 'view', 'gnudoor' );
		JRequest::setVar( 'layout', 'default_step3_paid');
		JRequest::setVar( 'cid', $cid);
		JRequest::setVar( 'txn', $txn_id);
		JRequest::setVar( 'txnDate', $txn_date);
		JRequest::setVar( 'txnAmount', $txn_amount);
		
		$serpost = $this->_savePost(JRequest::get('post'));
		$encodepost = $this->_encode($serpost, 'this is the key');
		JRequest::setVar( 'txnP', $encodepost);
		
		parent::display();		
	}
	function _formatValue($number = null) {
		return number_format($number, 2, '.', '');
	}
	
	function _savePost($array = null) {
	    // Save this in the login form
	    if (isset($array)) {
	    	$savePost = htmlspecialchars(serialize($array));
	    	return($savePost);
	    }
	}
	
	function _restorePost($savePost = null) {
	    if (isset($savePost)) {
	        $restorePost = unserialize(stripslashes(htmlspecialchars_decode($savePost)));
			return($restorePost);
	    }
	}
	
	function _encode($string,$key) {
	    $key = sha1($key);
	    $strLen = strlen($string);
	    $keyLen = strlen($key);
	    for ($i = 0; $i < $strLen; $i++) {
	        $ordStr = ord(substr($string,$i,1));
	        if ($j == $keyLen) { $j = 0; }
	        $ordKey = ord(substr($key,$j,1));
	        $j++;
	        $hash .= strrev(base_convert(dechex($ordStr + $ordKey),16,36));
	    }
	    return $hash;
	}
	
	function _decode($string,$key) {
	    $key = sha1($key);
	    $strLen = strlen($string);
	    $keyLen = strlen($key);
	    for ($i = 0; $i < $strLen; $i+=2) {
	        $ordStr = hexdec(base_convert(strrev(substr($string,$i,2)),36,16));
	        if ($j == $keyLen) { $j = 0; }
	        $ordKey = ord(substr($key,$j,1));
	        $j++;
	        $hash .= chr($ordStr - $ordKey);
	    }
	    return $hash;
	}

	function _sendToPaypal($cid = null, $invoice = null, $userid = null, $value = null){

		global $mainframe, $option;

		$component_params =& JComponentHelper::getParams($option);		

		$location = '';
		$location .= 'Location: https://www.sandbox.paypal.com/cgi-bin/webscr?';
		$location .= 'business=' . $component_params->get('paypal_email') . '&';
		$location .= 'return='.urlencode(JURI::base().'?option=' . $option . '&controller=pay&task=activate&cid=' . $cid ).'&';
		$location .= 'cancel_return='.urlencode(JURI::base().'?option=' . $option . '&controller=pay&task=cancel').'&';
		$location .= 'notify_url='.urlencode(JURI::base().'?option=' . $option . '&controller=pay&task=notify').'&';

		$location .= 'rm=2&';
		$location .= 'currency_code=' . $component_params->get('paypal_currency') . '&';
		$location .= 'lc=' . $component_params->get('paypal_currency') . '&';
		$location .= 'bn=gnudoorLLC_BuyNow_WPS_US&';
		$location .= "no_shipping=1&";
		$location .= "no_note=1&";
		
		$location .= 'item_name_1=Escrow Services&';

		$location .= 'amount_1=' . $value . '&';

		$location .= 'quantity_1=1&';
		$location .= 'invoice=' . $invoice . '&';
		
		$location .= 'custom=' . $userid . '&';
		$location .= 'upload=1&';
		$location .= 'cmd=_ext-enter&';
		$location .= 'redirect_cmd=_cart';
		
		header($location);
		
	}
	
    
}

?>